﻿#pragma once

#ifdef AS_STACK_WALKER
#include "StackWalker.h"
#ifdef _WIN32
#include <DbgHelp.h>
#endif

bool HookCrashReporter(bool logon);
void OutputCrashLogLine(const char *format, ...);

#ifdef _WIN32
namespace as
{
	namespace tools
	{
		class AsStackWalker : public StackWalker
		{
		public:
			virtual void OnSymInit(LPCSTR szSearchPath, DWORD symOptions, LPCSTR szUserName);
			virtual void OnLoadModule(LPCSTR    img,
				LPCSTR    mod,
				DWORD64   baseAddr,
				DWORD     size,
				DWORD     result,
				LPCSTR    symType,
				LPCSTR    pdbName,
				ULONGLONG fileVersion);
			virtual void OnCallstackEntry(CallstackEntryType eType, CallstackEntry &entry);
			virtual void OnDbgHelpErr(LPCSTR szFuncName, DWORD gle, DWORD64 addr);
			virtual void OnOutput(LPCSTR szText);
		};
	}
}

void StartCrashHandler();
void OnCrash(bool Terminate);

typedef struct _EXCEPTION_POINTERS EXCEPTION_POINTERS, *PEXCEPTION_POINTERS;
int __cdecl HandleCrash(PEXCEPTION_POINTERS pExceptPtrs);
void PrintCrashInformation(PEXCEPTION_POINTERS except);

#define THREAD_TRY_EXECUTION __try
#define THREAD_HANDLE_CRASH  __except(HandleCrash(GetExceptionInformation())) {}

#define THREAD_TRY_EXECUTION2 __try {
#define THREAD_HANDLE_CRASH2  } __except(HandleCrash(GetExceptionInformation())) {}

#endif
#endif
